// pages/classfli/classfli.js
const app = getApp()
var url = require('../../utils/url.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cate: [],
    classify: 1,
    toView: 'inToView01',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     console.log(111)
  },
  toSearch:function(){
    wx.navigateTo({
      url: '/pages/searchList/searchList?type=1',
    })
  },
  fenlei: function (e) {
    var index = e.currentTarget.dataset.index;
    var _id = e.currentTarget.dataset.id;
    this.setData({
      classify: _id,
      toView: 'inToView' + _id,
    })
    // console.log(_id)
  },
  getCate(){
    var that = this;
    wx.showLoading({
      title: '加载中..',
    })
    wx.request({
      url: url.URl + 'cate',
      data: {},
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var msg = res.data.msg;
        if (res.data.code == 200) {
          // console.log(res)
          that.setData({
            cate: res.data.data
          })
          wx.hideLoading()
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  //滚动触发
  chufa: function (e) {
    var that = this;
    var scrollTop = e.detail.scrollTop ;
    console.log(scrollTop,h)
    var h = 0;
    var classify;
    // console.log(that.data.cate)
    that.data.cate.forEach(function (csy, i) {
      var len = that.length(csy['id'])
      if (len > 3) {
        var _h = 50 +  (110 * Math.ceil(len/3)+33)
      } else {
        var _h = 50 +  110+33
      }

      console.log(scrollTop, _h)
      // console.log(i,_h)
      if (scrollTop >= h) {
        // console.log(csy)
        classify = csy['id'];
        console.log(classify)
      }
      h += _h;
    });
    
    that.setData({
      classify: classify
    })
  },
  length: function (e) {
    var that = this;
    var fenlei = that.data.cate;
    // console.log(e)
    for (var i = 0; i < fenlei.length; i++) {
      if (fenlei[i]['id'] == e) {
        return fenlei[i]['sub'].length
      }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getCate()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})